package org.seek.nirvana.nosql;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoDB
{
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
		{
			Mongo mongo = new Mongo("localhost");
			DB report = mongo.getDB("report");

			DBCollection coll = report.getCollection("report");
//			BasicDBObject doc = new BasicDBObject();
//
//			doc.put("name", "MongoDB");
//			doc.put("type", "database");
//			doc.put("count", 1);
//			doc.put("date", new Date());
//			coll.insert(doc);
//
//			coll.insert(doc);
//			DBObject myDoc = coll.findOne();
//			System.out.println(myDoc);
			
			System.out.println(coll.getFullName());
			
			long i =coll.getCount();
			System.out.println("count : "+i);
			
			DBCursor cursor=coll.find();
			while(cursor.hasNext())
			{
				DBObject ob = cursor.next();
				System.out.println(ob);
//				if(ob.containsField("date"))
//				{
//					Date date=(Date) ob.get("date");
//					System.out.println(date);
//				}
			}
			System.out.println("=============================");
			DBObject dbo=new BasicDBObject();
			dbo.put("date", -1);
			DBCursor cursor2=coll.find().sort(dbo);
			while(cursor2.hasNext())
			{
				DBObject ob = cursor2.next();
				System.out.println(ob);
//				if(ob.containsField("date"))
//				{
//					Date date=(Date) ob.get("date");
//					System.out.println(date);
//				}
			}
			
			
		} catch (UnknownHostException e)
		{
			e.printStackTrace();
		} catch (MongoException e)
		{
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
	}
}
